

$(function () {
    // 获取当前页面的url
    var currentUrl = window.location.href;
    var pop_up_tag = "";
    // 环境切换
    $(".env_select button").on("click", function () {
        var tp = $(this).attr("tp");
        var local_url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: local_url,
            data: formData,
            method: "POST",
            loading_call_back: function () {
                if (tp == "dev") {
                    $(".dev_env .content").text("切换中，请稍后......")
                } else if (tp == "yf") {
                    $(".yf_env .content").text("切换中，请稍后......")
                }
            },
            success_call_back: function (content) {
                if (tp == "dev") {
                    $(".dev_env .content").text(content.status)
                    $(".yf_env .content").text("未应用")
                } else if (tp == "yf") {
                    $(".dev_env .content").text("未应用")
                    $(".yf_env .content").text(content.status)
                }
            },
            error_call_back: function (content) {
                if (tp == "dev") {
                    $(".dev_env .content").text(content.status)
                } else if (tp == "yf") {
                    $(".yf_env .content").text(content.status)
                }
            }
        })
    });

    // 数据库切换
    $(".sql button").on("click", function () {
        var tp = $(this).attr("tp");
        var local_url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: local_url,
            data: formData,
            method: "POST",
            loading_call_back: function () {
                $(".sql .content").text("MYSQL连接中, 请稍后......")
            },
            success_call_back: function (content) {
                $(".sql .content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sql .content").text(content.status)

            }
        })
    });



    // PC端登录
    $(".pc_login").on("submit", function (e) {
        e.preventDefault();
        var login_url = $(this).attr("action");
        var username = $('#pcname').val();
        var password = $('#pcpd').val();
        const formData = {
            username: username,
            password: password,
            type: "pc_login"
        }
        djangoAjax({
            url: login_url,
            data: formData,
            method: "POST",
            loading_call_back: function () {
                $("#pc_login").text("登录中，请稍后......")
            },
            success_call_back: function (content) {
                $("#pc_login").text(content.status)
            },
            error_call_back: function (content) {
                $("#pc_login").text(content.status) // 显示错误提示
            }
        });
    });

    // APP端登录
    $(".app_login").on("submit", function (e) {
        e.preventDefault();
        var login_url = $(this).attr("action");
        var username = $('#appname').val();
        var password = $('#apppd').val();
        const formData = {
            username: username,
            password: password,
            type: "app_login"
        }
        djangoAjax({
            url: login_url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $("#app_login").text("登录中，请稍后......")
            },
            success_call_back: function (content) {
                $("#app_login").text(content.status)
            },
            error_call_back: function (content) {
                $("#app_login").text(content.status) // 显示错误提示
            }
        });
    });

    $(".prepose_data button").each(function (index, element) {
        $(this).on("click", function () {
            var next = $(this).next("span.env");
            var contentExement = next.find(".content");
            var tp = $(this).attr("tp");
            var local_url = currentUrl;
            const formData = {
                type: tp
            };
            djangoAjax({
                url: local_url,
                data: formData,
                method: "POST",
                loading_call_back: function () {
                    contentExement.text("执行中, 请稍后......");
                },
                success_call_back: function (content) {
                    contentExement.text(content.status)
                },
                error_call_back: function (content) {
                    contentExement.text(content.status)
                }
            })
        })
    })

    // 销售订单查询
    $(".sales_order_query").on("submit", function (e) {
        e.preventDefault();
        var query_url = $(this).attr("action");
        var order_no = $('#sales_order_no').val();
        const formData = {
            order_no: order_no,
            type: "sales_order_query"
        }
        if (order_no == "") {
            alert("请输入订单号");
        } else {
            djangoAjax({
                url: query_url,
                method: "POST",
                data: formData,
                loading_call_back: function () {
                    $(".sales_status_content").text("查询中，请稍后......")
                },
                success_call_back: function (content) {
                    $(".sales_status_content").text(content.status)
                },
                error_call_back: function (content) {
                    $(".sales_status_content").text(content.status) // 显示错误提示
                }
            });
        }
    });

    // 装车单操作
    $(".transport_order_query").on("submit", function (e) {
        e.preventDefault();
        var query_url = $(this).attr("action");
        var transport_no = $('#transport_order_no').val();
        const formData = {
            transport_no: transport_no,
            type: "transport_order_query"
        }
        if (transport_no == "") {
            alert("请输入订单号");
        } else {
            djangoAjax({
                url: query_url,
                method: "POST",
                data: formData,
                loading_call_back: function () {
                    $(".transport_status_content").text("查询中，请稍后......")
                },
                success_call_back: function (content) {
                    $(".transport_status_content").text(content.status)
                },
                error_call_back: function (content) {
                    $(".transport_status_content").text(content.status) // 显示错误提示
                }
            });
        }
    });



    $(".transport_order .order_operation_list button").on("click", function () {
        var tp = $(this).attr("tp");
        var query_url = $(".transport_order_query form").attr("action");
        var transport_no = $('#transport_order_no').val();
        console.log("no: " + transport_no)
        const formData = {
            transport_no: transport_no,
            type: tp
        }
        djangoAjax({
            url: query_url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".transport_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".transport_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".transport_status_content").text(content.status) // 显示错误提示
            }
        });
    })

    // 销售端重新判断
    $(".sales_order .order_operation_list .sales_reset").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".transport_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".transport_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".transport_status_content").text(content.status) // 显示错误提示
            }
        });

    })

    $(".sales_order .order_operation_list .set_ps_address").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        });
    })

    // 启用一级合约
    $(".sales_operation .set_one_hy_rule").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

    // 启用二级合约
    $(".sales_operation .set_two_hy_rule").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

    // 设置合约地址
    $(".sales_operation .set_hy_address").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

    // 设置配送地址
    $(".sales_operation .set_ps_rule").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

    // 预售操作
    $(".sales_order .order_operation_list .presell button").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

    // 配送预约
    $(".sales_order .order_operation_list .delivery_appointment button").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                if (tp == "get_driver_iphone") {
                    $(".sales_order .sales_status_content").text("手机号获取成功");
                    $(".sales_order .order_operation_list .delivery_appointment .driver_phone").text(content.status);
                } else {
                    $(".sales_order .sales_status_content").text(content.status)
                }
            },
            error_call_back: function (content) {
                if (tp == "get_driver_iphone") {
                    $(".sales_order .sales_status_content").text("获取手机号请求异常")
                } else {
                    $(".sales_order .sales_status_content").text(content.status)
                }
            }
        })
    })

    $(".logistics_order .order_operation_list .create_transport_order_masking").click(function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                if (tp == "get_driver_iphone") {
                    $(".sales_order .sales_status_content").text("手机号获取成功");
                    $(".sales_order .order_operation_list .delivery_appointment .driver_phone").text(content.status);
                } else {
                    $(".sales_order .sales_status_content").text(content.status)
                }
            },
            error_call_back: function (content) {
                if (tp == "get_driver_iphone") {
                    $(".sales_order .sales_status_content").text("获取手机号请求异常")
                } else {
                    $(".sales_order .sales_status_content").text(content.status)
                }
            }
        })
    })
    // 物流单查询
    $(".logistics_order_query").on("submit", function (e) {
        e.preventDefault();
        var query_url = currentUrl;
        var logistics_order_no = $('#logistics_order_no').val();
        const formData = {
            logistics_order_no: logistics_order_no,
            type: "logistics_order_query"
        }
        if (logistics_order_no == "") {
            alert("请输入订单号");
        } else {
            djangoAjax({
                url: query_url,
                method: "POST",
                data: formData,
                loading_call_back: function () {
                    $(".logistics_status_content").text("查询中，请稍后......")
                },
                success_call_back: function (content) {
                    $(".logistics_status_content").text(content.status)
                },
                error_call_back: function (content) {
                    $(".logistics_status_content").text(content.status) // 显示错误提示
                }
            });
        }
    });
    $(".sales_order .order_operation_list .untread button").on("click", function () {
        var tp = $(this).attr("tp");
        var url = currentUrl;
        const formData = {
            type: tp
        }
        djangoAjax({
            url: url,
            method: "POST",
            data: formData,
            loading_call_back: function () {
                $(".sales_order .sales_status_content").text("请求中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".sales_order .sales_status_content").text(content.status)
            }
        })
    })

})